package org.gap.service;

import java.util.List;
import java.util.Set;

import org.gap.dao.AdressesDAO;
import org.gap.dao.CodesPostauxDAO;
import org.gap.dao.PaysDAO;

import org.gap.domain.Adresses;
import org.gap.domain.CodesPostaux;
import org.gap.domain.Pays;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for CodesPostaux entities
 * 
 */

@Service("CodesPostauxService")
@Transactional
public class CodesPostauxServiceImpl implements CodesPostauxService {

	/**
	 * DAO injected by Spring that manages Adresses entities
	 * 
	 */
	@Autowired
	private AdressesDAO adressesDAO;

	/**
	 * DAO injected by Spring that manages CodesPostaux entities
	 * 
	 */
	@Autowired
	private CodesPostauxDAO codesPostauxDAO;

	/**
	 * DAO injected by Spring that manages Pays entities
	 * 
	 */
	@Autowired
	private PaysDAO paysDAO;

	/**
	 * Instantiates a new CodesPostauxServiceImpl.
	 *
	 */
	public CodesPostauxServiceImpl() {
	}

	/**
	 * Save an existing Pays entity
	 * 
	 */
	@Transactional
	public CodesPostaux saveCodesPostauxPays(String cdpPays, String cdpCodePostal, String cdpNInsee, String cdpLocalite, Pays related_pays) {
		CodesPostaux codespostaux = codesPostauxDAO.findCodesPostauxByPrimaryKey(cdpPays, cdpCodePostal, cdpNInsee, cdpLocalite, -1, -1);
		Pays existingpays = paysDAO.findPaysByPrimaryKey(related_pays.getPppPays());

		// copy into the existing record to preserve existing relationships
		if (existingpays != null) {
			existingpays.setPppPays(related_pays.getPppPays());
			existingpays.setPppLibelle(related_pays.getPppLibelle());
			existingpays.setPppMasqueCodePostal(related_pays.getPppMasqueCodePostal());
			existingpays.setPppMasqueTelephone(related_pays.getPppMasqueTelephone());
			existingpays.setPppPrefixeInternational(related_pays.getPppPrefixeInternational());
			existingpays.setPppLibelleRecherche(related_pays.getPppLibelleRecherche());
			existingpays.setPppCpModifiable(related_pays.getPppCpModifiable());
			existingpays.setPppNInsee(related_pays.getPppNInsee());
			existingpays.setPppEtiquetteRib(related_pays.getPppEtiquetteRib());
			existingpays.setPppMasquePortable(related_pays.getPppMasquePortable());
			existingpays.setPppMasqueEtablissement(related_pays.getPppMasqueEtablissement());
			existingpays.setPppMasqueGuichet(related_pays.getPppMasqueGuichet());
			existingpays.setPppMasqueCompte(related_pays.getPppMasqueCompte());
			existingpays.setPppMasqueCle(related_pays.getPppMasqueCle());
			existingpays.setPppOrdreDsIban(related_pays.getPppOrdreDsIban());
			existingpays.setPppCe(related_pays.getPppCe());
			related_pays = existingpays;
		}

		codespostaux.setPays(related_pays);
		related_pays.getCodesPostauxes().add(codespostaux);
		codespostaux = codesPostauxDAO.store(codespostaux);
		codesPostauxDAO.flush();

		related_pays = paysDAO.store(related_pays);
		paysDAO.flush();

		return codespostaux;
	}

	/**
	 * Delete an existing Pays entity
	 * 
	 */
	@Transactional
	public CodesPostaux deleteCodesPostauxPays(String codespostaux_cdpPays, String codespostaux_cdpCodePostal, String codespostaux_cdpNInsee, String codespostaux_cdpLocalite, String related_pays_pppPays) {
		CodesPostaux codespostaux = codesPostauxDAO.findCodesPostauxByPrimaryKey(codespostaux_cdpPays, codespostaux_cdpCodePostal, codespostaux_cdpNInsee, codespostaux_cdpLocalite, -1, -1);
		Pays related_pays = paysDAO.findPaysByPrimaryKey(related_pays_pppPays, -1, -1);

		codespostaux.setPays(null);
		related_pays.getCodesPostauxes().remove(codespostaux);
		codespostaux = codesPostauxDAO.store(codespostaux);
		codesPostauxDAO.flush();

		related_pays = paysDAO.store(related_pays);
		paysDAO.flush();

		paysDAO.remove(related_pays);
		paysDAO.flush();

		return codespostaux;
	}

	/**
	 * Return all CodesPostaux entity
	 * 
	 */
	@Transactional
	public List<CodesPostaux> findAllCodesPostauxs(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<CodesPostaux>(codesPostauxDAO.findAllCodesPostauxs(startResult, maxRows));
	}

	/**
	 * Save an existing Adresses entity
	 * 
	 */
	@Transactional
	public CodesPostaux saveCodesPostauxAdresseses(String cdpPays, String cdpCodePostal, String cdpNInsee, String cdpLocalite, Adresses related_adresseses) {
		CodesPostaux codespostaux = codesPostauxDAO.findCodesPostauxByPrimaryKey(cdpPays, cdpCodePostal, cdpNInsee, cdpLocalite, -1, -1);
		Adresses existingadresseses = adressesDAO.findAdressesByPrimaryKey(related_adresseses.getAddAdresse());

		// copy into the existing record to preserve existing relationships
		if (existingadresseses != null) {
			existingadresseses.setAddAdresse(related_adresseses.getAddAdresse());
			existingadresseses.setAddTopNpai(related_adresseses.getAddTopNpai());
			existingadresseses.setAddTypeVoie(related_adresseses.getAddTypeVoie());
			existingadresseses.setAddLigne1(related_adresseses.getAddLigne1());
			existingadresseses.setAddLigne2(related_adresseses.getAddLigne2());
			existingadresseses.setAddLigne3(related_adresseses.getAddLigne3());
			existingadresseses.setAddBureauDistrib(related_adresseses.getAddBureauDistrib());
			existingadresseses.setAddDateCreation(related_adresseses.getAddDateCreation());
			existingadresseses.setAddDateModification(related_adresseses.getAddDateModification());
			related_adresseses = existingadresseses;
		} else {
			related_adresseses = adressesDAO.store(related_adresseses);
			adressesDAO.flush();
		}

		related_adresseses.setCodesPostaux(codespostaux);
		codespostaux.getAdresseses().add(related_adresseses);
		related_adresseses = adressesDAO.store(related_adresseses);
		adressesDAO.flush();

		codespostaux = codesPostauxDAO.store(codespostaux);
		codesPostauxDAO.flush();

		return codespostaux;
	}

	/**
	 * Load an existing CodesPostaux entity
	 * 
	 */
	@Transactional
	public Set<CodesPostaux> loadCodesPostauxs() {
		return codesPostauxDAO.findAllCodesPostauxs();
	}

	/**
	 * Save an existing CodesPostaux entity
	 * 
	 */
	@Transactional
	public void saveCodesPostaux(CodesPostaux codespostaux) {
		CodesPostaux existingCodesPostaux = codesPostauxDAO.findCodesPostauxByPrimaryKey(codespostaux.getCdpPays(), codespostaux.getCdpCodePostal(), codespostaux.getCdpNInsee(), codespostaux.getCdpLocalite());

		if (existingCodesPostaux != null) {
			if (existingCodesPostaux != codespostaux) {
				existingCodesPostaux.setCdpPays(codespostaux.getCdpPays());
				existingCodesPostaux.setCdpCodePostal(codespostaux.getCdpCodePostal());
				existingCodesPostaux.setCdpNInsee(codespostaux.getCdpNInsee());
				existingCodesPostaux.setCdpLocalite(codespostaux.getCdpLocalite());
				existingCodesPostaux.setCdpBureauDistributeur(codespostaux.getCdpBureauDistributeur());
				existingCodesPostaux.setCdpParticuCommuneLieuDit(codespostaux.getCdpParticuCommuneLieuDit());
				existingCodesPostaux.setCdpParticuBureauDistrib(codespostaux.getCdpParticuBureauDistrib());
				existingCodesPostaux.setCdpCodeMiseAJour(codespostaux.getCdpCodeMiseAJour());
				existingCodesPostaux.setCdpLocaliteFiscalStandard(codespostaux.getCdpLocaliteFiscalStandard());
			}
			codespostaux = codesPostauxDAO.store(existingCodesPostaux);
		} else {
			codespostaux = codesPostauxDAO.store(codespostaux);
		}
		codesPostauxDAO.flush();
	}

	/**
	 * Return a count of all CodesPostaux entity
	 * 
	 */
	@Transactional
	public Integer countCodesPostauxs() {
		return ((Long) codesPostauxDAO.createQuerySingleResult("select count(*) from CodesPostaux o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing CodesPostaux entity
	 * 
	 */
	@Transactional
	public void deleteCodesPostaux(CodesPostaux codespostaux) {
		codesPostauxDAO.remove(codespostaux);
		codesPostauxDAO.flush();
	}

	/**
	 */
	@Transactional
	public CodesPostaux findCodesPostauxByPrimaryKey(String cdpPays, String cdpCodePostal, String cdpNInsee, String cdpLocalite) {
		return codesPostauxDAO.findCodesPostauxByPrimaryKey(cdpPays, cdpCodePostal, cdpNInsee, cdpLocalite);
	}

	/**
	 * Delete an existing Adresses entity
	 * 
	 */
	@Transactional
	public CodesPostaux deleteCodesPostauxAdresseses(String codespostaux_cdpPays, String codespostaux_cdpCodePostal, String codespostaux_cdpNInsee, String codespostaux_cdpLocalite, Integer related_adresseses_addAdresse) {
		Adresses related_adresseses = adressesDAO.findAdressesByPrimaryKey(related_adresseses_addAdresse, -1, -1);

		CodesPostaux codespostaux = codesPostauxDAO.findCodesPostauxByPrimaryKey(codespostaux_cdpPays, codespostaux_cdpCodePostal, codespostaux_cdpNInsee, codespostaux_cdpLocalite, -1, -1);

		related_adresseses.setCodesPostaux(null);
		codespostaux.getAdresseses().remove(related_adresseses);

		adressesDAO.remove(related_adresseses);
		adressesDAO.flush();

		return codespostaux;
	}
}
